package com.zpl.common.util;

import com.alibaba.fastjson.JSON;
import com.zpl.common.rpc.RpcMessageDto;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/9/9 13:26
 */
public class DelimiterUtil {
    
    private DelimiterUtil() {}

    /**
     * 分隔符
     */
    public static final String DELIMITER = "~!@#$%^&*";

    /**
     * 长度
     * 
     * 避免把缓冲区打包
     */
    public static final int LENGTH = 65535;

    /**
     * 分隔符 buffer
     */
    public static final ByteBuf DELIMITER_BUF = Unpooled.copiedBuffer(DELIMITER.getBytes());

    /**
     * 获取对应的字节缓存
     * @param text 文本
     * @return
     */
    public static ByteBuf getByteBuf(String text) {
        return Unpooled.copiedBuffer(text.getBytes());
    }
    
    public static ByteBuf getMessageDelimiterBuffer(RpcMessageDto rpcMessageDto) {
        String json = JSON.toJSONString(rpcMessageDto);
        String jsonDelimiter = json + DELIMITER;
        
        return Unpooled.copiedBuffer(jsonDelimiter.getBytes());
    }
}
